package com.smallcc.controller;

import com.smallcc.converter.OrderForm2OrderDTOConverter;
import com.smallcc.dto.OrderDTO;
import com.smallcc.enums.ResultEnum;
import com.smallcc.exception.SellException;
import com.smallcc.form.OrderForm;
import com.smallcc.service.BuyerService;
import com.smallcc.service.OrderService;
import com.smallcc.util.ResultVoUtil;
import com.smallcc.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 晓风过境
 * @TODO 买家订单管理
 * @date 2018/3/29 22:18
 * @day 29
 * @return $text$
 * @title ${CLASS_NAME}
 * @project_name sell
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;


    //创建订单
    @PostMapping("/create")
    public ResultVO<Map<String,String>> createOrder(@Valid OrderForm orderForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("[创建订单]参数不正确,orderForm={}",orderForm);
            throw new SellException(ResultEnum.ORDER_NOT_EXIST.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }

        OrderDTO orderDTO= OrderForm2OrderDTOConverter.converter(orderForm);
        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.error("[创建订单]购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDTO creatResult = orderService.create(orderDTO);
        Map<String,String> map=new HashMap<>();
        map.put("orderId",creatResult.getOrderId());

        return ResultVoUtil.sucess(map);
    }
    //订单列表
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openId, @RequestParam(value = "page",defaultValue = "0") Integer page, @RequestParam(value="size",defaultValue = "10") Integer size){
        if(StringUtils.isEmpty(openId)){
            log.error("[查询订单列表]openid不为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest request=new PageRequest(page,size);

        Page<OrderDTO> orderDTOPage = orderService.findList(openId, request);
        return ResultVoUtil.sucess(orderDTOPage.getContent());
//        return ResultVoUtil.sucess();
    }


    //订单详情
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openId,@RequestParam("orderId") String orderId) {
        OrderDTO orderDTO = buyerService.findOrderOne(openId, orderId);
        return ResultVoUtil.sucess(orderDTO);
    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openId,@RequestParam("orderId") String orderId) {
        buyerService.cancelOrder(openId, orderId);
        return ResultVoUtil.sucess();
    }
}
